package com.ygp.cas;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * <Description> <br>
 *
 * @author linmiaolai@sanyygp.com<br>
 * @version 1.0<br>
 * @date 2021/08/30 <br>
 */
public class UnSafeInteger {
    private int count = 0;

    private int getAndIncrement() {
        count += 1;
        return count;
    }


    public static void main(String[] args) throws InterruptedException {
        safeTest();
    }

    private void unsafeTest() throws InterruptedException {
        UnSafeInteger unSafeInteger = new UnSafeInteger();

       /* Thread th1 = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                unSafeInteger.getAndIncrement();
            }
        });


        Thread th2 = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                unSafeInteger.getAndIncrement();
            }
        });

        th1.start();
        th2.start();
        th1.join();
        th2.join();*/
        System.out.println(unSafeInteger.count);
    }
    private static void safeTest() throws InterruptedException {
        AtomicInteger atomicInteger = new AtomicInteger();
        /*Thread th1 = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                atomicInteger.getAndIncrement();
            }
        });


        Thread th2 = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                atomicInteger.getAndIncrement();
            }
        });
        th1.start();
        th2.start();
        th1.join();
        th2.join();
        System.out.println(atomicInteger.get());*/
    }
}
